home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / fnmatch.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2008-10-13  |  1.9 KB  |  90 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import re
  5. __all__ = [
  6.     'filter',
  7.     'fnmatch',
  8.     'fnmatchcase',
  9.     'translate']
  10. _cache = { }
  11.  
  12. def fnmatch(name, pat):
  13.     import os
  14.     name = os.path.normcase(name)
  15.     pat = os.path.normcase(pat)
  16.     return fnmatchcase(name, pat)
  17.  
  18.  
  19. def filter(names, pat):
  20.     import os
  21.     import posixpath
  22.     result = []
  23.     pat = os.path.normcase(pat)
  24.     if pat not in _cache:
  25.         res = translate(pat)
  26.         _cache[pat] = re.compile(res)
  27.     
  28.     match = _cache[pat].match
  29.     if os.path is posixpath:
  30.         for name in names:
  31.             if match(name):
  32.                 result.append(name)
  33.                 continue
  34.         
  35.     else:
  36.         for name in names:
  37.             if match(os.path.normcase(name)):
  38.                 result.append(name)
  39.                 continue
  40.         
  41.     return result
  42.  
  43.  
  44. def fnmatchcase(name, pat):
  45.     if pat not in _cache:
  46.         res = translate(pat)
  47.         _cache[pat] = re.compile(res)
  48.     
  49.     return _cache[pat].match(name) is not None
  50.  
  51.  
  52. def translate(pat):
  53.     i = 0
  54.     n = len(pat)
  55.     res = ''
  56.     while i < n:
  57.         c = pat[i]
  58.         i = i + 1
  59.         if c == '*':
  60.             res = res + '.*'
  61.             continue
  62.         if c == '?':
  63.             res = res + '.'
  64.             continue
  65.         if c == '[':
  66.             j = i
  67.             if j < n and pat[j] == '!':
  68.                 j = j + 1
  69.             
  70.             if j < n and pat[j] == ']':
  71.                 j = j + 1
  72.             
  73.             while j < n and pat[j] != ']':
  74.                 j = j + 1
  75.             if j >= n:
  76.                 res = res + '\\['
  77.             else:
  78.                 stuff = pat[i:j].replace('\\', '\\\\')
  79.                 i = j + 1
  80.                 if stuff[0] == '!':
  81.                     stuff = '^' + stuff[1:]
  82.                 elif stuff[0] == '^':
  83.                     stuff = '\\' + stuff
  84.                 
  85.                 res = '%s[%s]' % (res, stuff)
  86.         j >= n
  87.         res = res + re.escape(c)
  88.     return res + '$'
  89.  
  90.